<!--
	Copyright (C) 2010 Ethan Gruber
	Numishare: http://code.google.com/p/numishare/
	Apache License 2.0
	
	Heavily modified from upload form developed by Orbeon	
-->
<html xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns="http://www.w3.org/1999/xhtml" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fr="http://orbeon.org/oxf/xml/form-runner" xmlns:xi="http://www.w3.org/2001/XInclude">
	<head>
		<title>Numishare: Manage CSV Imports</title>
		<!-- Core + Skin CSS -->
		<xhtml:link rel="stylesheet" type="text/css" href="/apps/numishare/css/jquery-ui-1.8.10.custom.css"/>
		<xhtml:link rel="stylesheet" href="/apps/numishare/css/reset-fonts-grids.css"/>
		<xhtml:link rel="stylesheet" href="/apps/numishare/css/base-min.css"/>
		<xhtml:link rel="stylesheet" href="/apps/numishare/css/fonts-min.css"/>

		<!-- numishare styling -->
		<xhtml:link rel="stylesheet" href="/apps/numishare/css/xforms.css"/>

		<xhtml:script type="text/javascript" src="/apps/numishare/javascript/jquery-1.6.1.min.js"/>
		<xhtml:script type="text/javascript" src="/apps/numishare/javascript/numishare-menu.js"/>
		<xforms:model>
			<!-- exist URL is stored in an XML file -->
			<xforms:instance id="exist-url">
				<xi:include href="exist-url.xml"/>
			</xforms:instance>
			
			<xforms:instance id="control-instance">
				<controls xmlns="">
					<simple-upload-trigger/>
					<add-upload-trigger/>
					<remove-upload-trigger/>
					<collection-name/>
				</controls>
			</xforms:instance>
			
			<xforms:instance id="dump">
				<dump/>
			</xforms:instance>
			
			<!-- collections/authentication instances -->
			<xforms:instance id="collections-list">
				<collections xmlns="">
					<collection roll="admin" name="numishare"/>
				</collections>
			</xforms:instance>
			
			<xforms:instance id="imports">
				<imports xmlns=""/>
			</xforms:instance>

			<xforms:instance id="timestamp">
				<timestamp xmlns=""/>
			</xforms:instance>

			<xforms:instance id="current-id">
				<id xmlns=""/>
			</xforms:instance>

			<!-- delete from Solr -->
			<xforms:instance id="deleteId">
				<delete xmlns="">
					<id/>
				</delete>
			</xforms:instance>

			<!-- Instance for Solr commit-->
			<xforms:instance id="sendCommit">
				<commit/>
			</xforms:instance>

			<!-- Solr optimize -->
			<xforms:instance id="optimizeIndex">
				<optimize/>
			</xforms:instance>

			<!-- load URLs from config.xml in eXist into form for Solr and CSS file -->
			<xforms:instance id="config">
				<config xmlns="">
					<url/>
					<solr_published/>
				</config>
			</xforms:instance>
			
			<xforms:submission id="load-collections" serialization="none" method="get" action="{instance('exist-url')}collections-list.xml" replace="instance" instance="collections-list"
				xxforms:username="admin" xxforms:password="">
				<!-- if the config loads successfully, set the collection names based on authentication -->
				<xforms:action ev:event="xforms-submit-done">
					<!-- set default if security is false -->
					<xforms:action if="not(string(instance('control-instance')/request-security/role))">
						<xforms:setvalue ref="instance('control-instance')/collection-name">numishare</xforms:setvalue>
					</xforms:action>
					<!-- if there is a security role, set the collection-name value if it is in the list, otherwise set new collection name -->
					<xforms:action if="string(instance('control-instance')/request-security/role)">
						<xforms:action if="string(instance('collections-list')/collection[@role=instance('control-instance')/request-security/role]/@name)">
							<xforms:setvalue ref="instance('control-instance')/collection-name"
								value="instance('collections-list')/collection[@role=instance('control-instance')/request-security/role]/@name"/>
						</xforms:action>
					</xforms:action>			
					<xforms:send submission="load-imports"/>
					<xforms:send submission="load-config"/>
				</xforms:action>
				<!-- if the config has not been created (given that the URL in exist-url.xml is correct), create it -->
				<xforms:message ev:event="xforms-submit-error" level="modal">Unable to load Numishare collections list.</xforms:message>
			</xforms:submission>

			<!-- load site info -->
			<xforms:submission id="load-config" serialization="none" method="get" action="{instance('exist-url')}{instance('control-instance')/collection-name}/config.xml" xxforms:username="admin" xxforms:password=""
				replace="instance" instance="config"/>

			<xforms:submission id="delete-submission" action="/exist/rest/numishare/objects/{instance('current-id')}.xml"  xxforms:username="admin" xxforms:password="" method="delete" replace="none">
				<xforms:send ev:event="xforms-submit-done" submission="delete-from-solr"/>
			</xforms:submission>

			<!-- delete from Solr -->
			<xforms:submission id="delete-from-solr" action="{instance('config')/solr_published}update" ref="instance('deleteId')" instance="deleteId" replace="none" method="post"/>

			<!-- send commit -->
			<xforms:submission id="submitCommit" action="{instance('config')/solr_published}update" ref="instance('sendCommit')" instance="sendCommit" replace="none" method="post">
				<xforms:message ev:event="xforms-submit-error" level="modal">Data Failed to POST to Solr!</xforms:message>
				<xforms:send submission="optimize" ev:event="xforms-submit-done"/>
			</xforms:submission>

			<xforms:submission id="load-imports" serialization="none" method="get" action="{instance('exist-url')}{instance('control-instance')/collection-name}/imports.xml" xxforms:username="admin" xxforms:password="" replace="instance"
				instance="imports"/>

			<xforms:submission id="save-imports" ref="instance('imports')" xxforms:username="admin" xxforms:password="" action="{instance('exist-url')}{instance('control-instance')/collection-name}/imports.xml" method="put" replace="none">
				<xforms:send submission="submitCommit" ev:event="xforms-submit-done"/>
				<xforms:setvalue ref="instance('status')" ev:event="xforms-submit-done">CSV import and associated files removed.</xforms:setvalue>
				<xforms:message ev:event="xforms-submit-error" level="modal">Error saving imports record.</xforms:message>
			</xforms:submission>

			<!-- send optimize -->
			<xforms:submission id="optimize" action="{instance('config')/solr_published}update" ref="instance('optimizeIndex')" instance="optimizeIndex" replace="none" method="post">
				<xforms:message ev:event="xforms-submit-error" level="modal">Failed to optimize Solr index.</xforms:message>
			</xforms:submission>

			<xforms:action ev:event="xforms-model-construct-done">
				<xforms:send submission="load-collections"/>
			</xforms:action>
		</xforms:model>
	</head>
	<xhtml:body class="yui-skin-sam">
		<xhtml:div id="doc4" class="yui-t2 content ui-corner-all">
			<xxforms:variable name="display_path">../</xxforms:variable>
			<xi:include href="header.xml"/>			
			<div id="bd">
				<xhtml:div id="form">
					<xforms:group ref="instance('status')/text()">
						<div class="success">
							<xforms:output ref="instance('status')"/>
						</div>
					</xforms:group>
					<h3>Imports</h3>
					<p><a href="new/">New Import</a></p>
					<xforms:group ref="instance('imports')">
						<xforms:group ref=".[count(import) &gt; 0]">
							<div class="section">							
								<table style="width:100%;font-size:12px;">
									<thead>
										<th>File Name</th>
										<th>Import Date</th>
										<th>Files</th>
										<th>Delete?</th>
									</thead>
									<tbody>
										<xforms:repeat nodeset="import">
											<xxforms:variable name="date" select="@date"/>
											<tr>
												<td>
													<xforms:output value="@filename"/>
												</td>
												<td>
													<xforms:output value="$date"/>
												</td>
												<td style="text-align:center">
													<xforms:output value="count(file)"/>
												</td>
												<td style="text-align:center">
													<xforms:trigger appearance="minimal">
														<xforms:action ev:event="DOMActivate">
															<xforms:setvalue ref="instance('timestamp')" value="$date"/>
															<xforms:dispatch target="remove-import-dialog" name="fr-show"/>
														</xforms:action> <xforms:label>
															<img src="{$display_path}images/remove.gif"/>
														</xforms:label>
													</xforms:trigger>
												</td>
											</tr>
										</xforms:repeat>
									</tbody>
								</table>
							</div>
						</xforms:group>
						<xforms:group ref=".[count(import) = 0]">
							<p>There have been no imports from CSV yet.</p>
						</xforms:group>
					</xforms:group>
					
					<fr:alert-dialog id="remove-import-dialog">
						<fr:label>Confirm Delete</fr:label>
						<fr:message> Are you sure you wish to undo this import? </fr:message>
						<fr:negative-choice/>
						<fr:positive-choice>
							<xforms:action ev:event="DOMActivate">
								<xforms:action xxforms:iterate="instance('imports')/import[@date = instance('timestamp')]/file">
									<xxforms:variable name="id" select="@id"/>
									<xforms:setvalue ref="instance('current-id')" value="$id"/>
									<xforms:setvalue ref="instance('deleteId')/id" value="$id"/>
									<xforms:send submission="delete-submission"/>
								</xforms:action>
								<xforms:delete context="instance('imports')/import[@date = instance('timestamp')]"/>
								<xforms:send submission="save-imports"/>
							</xforms:action>
						</fr:positive-choice>
					</fr:alert-dialog>
				</xhtml:div>
			</div>
			
			<!--<widget:xforms-instance-inspector id="orbeon-xforms-inspector" xmlns:widget="http://orbeon.org/oxf/xml/widget"/>-->
			
		</xhtml:div>		
	</xhtml:body>	
</html>